var magic = {};
magic.argumentType = "title";
magic.currentRuleButton = "";
magic.currentTemplateButton = "";
magic.dateFormat = 'yy-mm-dd';
magic.monthNames = ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];
magic.dayNamesMin = ['日','一','二','三','四','五','六'];
(function(product){
		var 
		_callback ={},
		rules = {
				"email" : "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"
		},
		
		//返回
		_goToFun = function(url,cateIndex,liIndex){
					window.location.href=url;
					parent.document.getElementById('nav').contentWindow.NAV.current(cateIndex,liIndex);
		},
		_validate = function(rule,id,message){
				var RegExpObject = new RegExp(rule);
				if (!RegExpObject.test($(id).val())) {
						if (message){
								alert(message);
								$(id).focus();
						}
						return false;
				}
				return true;
		};
		
		product.init = function(page, parms) {
				if(!parms) {
						parms = {};
				}
				
				if( typeof _callback[page] === "function" ) {
						_callback[page].call(this, parms);
				}
				else {
						alert("没有为页面" + page + "定义callback函数，请联系前端");
				}	
		};
		
    _callback["task_leader"] = function(parms) {
    };
		
    _callback["template_admin"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				$("#add-template").click(function(){
								_goToFun(parms.templateAddUrl,1,2);
								return false;
						}
				);
    };
		
    _callback["template_detail"] = function(parms) {
				$("#back").click(function(){
						_goToFun(parms.backUrl,1,1);
						return false;
				});
    };
		
    _callback["template_add"] = _callback["template_editor"] = function(parms) {
				$("#back").click(function(){
						_goToFun(parms.backUrl,1,1);
						return false;
				});
				$("#add-title-arguments").click(function(){
						var argumentsList = window.open(parms.argumentsListUrl);
						magic.argumentType = "title";
						return false;
				});
				$("#add-html-arguments").click(function(){
						var argumentsList = window.open(parms.argumentsListUrl);
						magic.argumentType = "html";
						return false;
				});
				$("#add-txt-arguments").click(function(){
						var argumentsList = window.open(parms.argumentsListUrl);
						magic.argumentType = "txt";
						return false;
				});
				$("#validate").submit(function(){
						var
						txtContent =	_validate("#txt-content","TXT模板内容不能为空！"),
						htmlContent =	_validate("#html-content","HTML模板内容不能为空！"),
						emailTitle =	_validate("#email-title","邮件标题不能为空！"),
						templateDesc =	_validate("#template-desc","模板描述不能为空！"),
						templateName = _validate("#template-name","模板名称不能为空！");
						if (!(templateName && templateDesc && emailTitle && htmlContent && txtContent)) { return false; }
				});
	 };
	
    _callback["template_test"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,1,1);
								return false;
						}
				);
				
				$("#validate").submit(function(){
						var arguments = $("#template-arguments input").get();
						for (var i = 0, j = arguments.length;i < j; i++){
								if (arguments[i].value == ""){
										alert("参数变量不能为空！");
										arguments[i].focus();
										arguments = false;
										i = j;
								}
						}
						var testUrl =	_validate(rules.email,"#test-url","请填写正确的测试邮箱地址！示例：\n");
						if (!(testUrl && arguments)) { return false; }
				});
	 };
	 
    _callback["task_rules_admin"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				$("#add-task-rule").click(function(){
								_goToFun(parms.taskRulesAddUrl,4,5);
								return false;
						}
				);
	 };
	 
    _callback["task_rules_detail"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,4,4);
								return false;
						}
				);
	 };
	 
    _callback["task_rules_add"] = _callback["task_rules_editor"] = function(parms) {
				$("#start-time,#end-time,#on-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin,minDate:parms.minDate});
				$("#cycle").hide();
				$("#definite-time").hide();
				
				$("#back").click(function(){
								_goToFun(parms.backUrl,4,4);
								return false;
						}
				);
				
				$(".clear").click(function(){
								this.parentNode.getElementsByTagName("input")[0].value = "";
								return false;
						}
				);
				
				$("#definite-time-radio").click(function(){
								if (this.checked){
										$("#cycle").hide();
										$("#definite-time").show();
								}
						}
				);
				
				$("#real-time-radio").click(function(){
								if (this.checked){
										$("#cycle").hide();
										$("#definite-time").hide();
								}
						}
				);
				
				$("#cycle-radio").click(function(){
								if (this.checked){
										$("#cycle").show();
										$("#definite-time").hide();
								}
						}
				);
				
				$(".select-template").click(function(){
						var templateList = window.open(parms.templateListUrl);
						magic.currentTemplateButton = this;
						return false;
				});
				
				$("#validate").submit(function(){
						if ($(".select-template").length > 1){
								var
								emailWay =	_validate("#email-way"),
								messageWay =	_validate("#message-way"),
								smsWay =	_validate("#sms-way");
								if(!(emailWay || messageWay || smsWay)) { alert("至少选择一种通道！"); $("#email-way").focus();};
								var way = emailWay || messageWay || smsWay;
						}else{
								var id = $(".select-template").parent().find("input")[0].id,
										way = _validate("#" + id,"通道选项不能为空！");
						}
						var ruleTitle = _validate("#rule-title","任务规则名称不能为空！");
						if (!(way && ruleTitle)) { return false; }
				});
	 };
	 
    _callback["task_admin"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				$("#add-task").click(function(){
								_goToFun(parms.taskAddUrl,6,7);
								return false;
						}
				);
	 };
	 
    _callback["task_add"] = _callback["task_editor"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,6,6);
								return false;
						}
				);
				
				$(".clear").click(function(){
						this.parentNode.getElementsByTagName("input")[0].value = "";
						return false;
				});
				
				$(".delete").each(function(i,that){
						$(that).addClass("binded");
						$(that).click(function(){
								if (confirm("确定要删除改规则吗？")){
										$(that.parentNode).remove();
								};
								return false;
						});
				});
				
				$(".select-task-rule").each(function(i,that){
						$(that).addClass("binded");
						$(that).click(function(){
								var taskRulesList = window.open(parms.taskRulesListUrl);
								magic.currentRuleButton = that;
								return false;
						});
				});
				
				$("#add-task-rule").click(function(){
						var taskRuleLength = $(".task-rules").length + 1;
						$("#task-rules-wrap").append('<div style="margin-top:10px;" class="task-rules"><input type="text" name="task-rule-'+ taskRuleLength +'" class="text s-size readonly" id="task-rule-'+ taskRuleLength +'" readonly="readonly"> <button class="select-task-rule">选择发送规则</button> <button class="clear">清空</button> <button class="delete">删除</button></div>');									
						$(".clear").click(function(){
								this.parentNode.getElementsByTagName("input")[0].value = "";
								return false;
						});
						$(".delete").each(function(i,that){
								if ($(that).hasClass("binded")){return;};
								$(that).addClass("binded");
								$(that).click(function(){
										if (confirm("确定要删除改规则吗？")){
												$(that.parentNode).remove();
										};
										return false;
								});
						});
						$(".select-task-rule").each(function(i,that){
								if ($(that).hasClass("binded")){return;};
								$(that).addClass("binded");
								$(that).click(function(){
										var taskRulesList = window.open(parms.taskRulesListUrl);
										magic.currentRuleButton = that;
										return false;
								});
						});
						return false;
				});
				
				$("#validate").submit(function(){
						var taskPeople =	_validate("#task-people","人群不能为空！"),
								rules = $("#task-rules-wrap input").get();
						if (rules.length == 0){
								alert("任务规则不能少于一项！");
								rules = false;
						}else{
								for (var i = 0, j = rules.length;i < j; i++){
										if (rules[i].value == ""){
												alert("任务规则不能为空！");
												rules[i].focus();
												rules = false;
												i = j;
										}
								}
						}
						var taskDesc =	_validate("#task-desc","任务描述不能为空！"),
								taskTitle =	_validate("#task-title","任务标题不能为空！");
						if (!(taskPeople && rules && taskDesc && taskTitle)) { return false; }
				});
	 };
	 
    _callback["task_stat"] = function(parms) {
				$("#back").click(function(){
						_goToFun(parms.backUrl,6,6);
						return false;
				});
	 };
	 
    _callback["arguments_admin"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				$("#add-arguments").click(function(){
								_goToFun(parms.argumentsAddUrl,9,10);
								return false;
						}
				);
	 };
	 
    _callback["arguments_detail"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,9,9);
								return false;
						}
				);
	 };
	 
    _callback["arguments_add"] = _callback["arguments_editor"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,9,9);
								return false;
						}
				);
				$("#validate").submit(function(){
						var
						argumentDesc = _validate("#argument-desc","参数描述不能为空！"),
						argumentVariant =	_validate("#argument-variant","参数变量不能为空！"),
						argumentTitle =	_validate("#argument-title","参数名称不能为空！");
						if (!(argumentTitle && argumentVariant && argumentDesc)) { return false; }
				});
	 };
	 
    _callback["black_admin"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				$("#add-black").click(function(){
								_goToFun(parms.blackAddUrl,11,12);
								return false;
						}
				);
	 };
	 
    _callback["black_add"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,11,11);
								return false;
						}
				);
				$("#validate").submit(function(){
						var
						blackDesc = _validate("#black-desc","描述不能为空！"),
						blackUrl =	_validate("#black-url","地址不能为空！");
						if (!(blackDesc && blackUrl)) { return false; }
				});
	 };
	 
    _callback["black_editor"] = function(parms) {
				$("#back").click(function(){
								_goToFun(parms.backUrl,11,11);
								return false;
						}
				);
				$("#validate").submit(function(){
						var
						blackDesc = _validate("#black-desc","描述不能为空！");
						if (!(blackDesc)) { return false; }
				});
	 };
	 
    _callback["arguments_list"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				$(".add-argument").click(function(){
						if (window.opener.magic.argumentType == "html"){
								targetTextarea = window.opener.document.getElementById("html-content");
						}else if(window.opener.magic.argumentType == "txt"){
								targetTextarea = window.opener.document.getElementById("txt-content");
						}else if(window.opener.magic.argumentType == "title"){
								targetTextarea = window.opener.document.getElementById("email-title");
						}else{
								return;
						}
						targetTextarea.value = targetTextarea.value + "[" + this.getAttribute("data") + "]";
						window.close();
				});
	 };
	 
    _callback["task_rules_list"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				$(".select-task-rule").click(function(){
						targetInput = window.opener.magic.currentRuleButton.parentNode.getElementsByTagName("input")[0];
						targetInput.value = this.getAttribute("data");
						window.close();
				});
	 };
	 
    _callback["template_list"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				$(".select-template").click(function(){
						targetInput = window.opener.magic.currentTemplateButton.parentNode.getElementsByTagName("input")[0];
						targetInput.value = this.getAttribute("data");
						window.close();
				});
	 };
	 
	 
	 
	 //=================================积分兑换================================//
    _callback["gift-center"] = function(parms) {
				$("#start-time,#end-time").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				$("#checkall").click(function(){
						if($(this).attr("checked")){
							$(":checkbox").attr("checked",true);
						}else{
							$(":checkbox").attr("checked",false);
						}
				});
				$(":checkbox").click(function(){
						if(!$(this).attr("checked")){
								$("#checkall").attr("checked",false);
						}else{
							var checkAll = true;
							$("td :checkbox").each(function(){
							 	if(!$(this).attr("checked")){
									checkAll = false;
									return;
								}
						 	});
							if(checkAll){
								$("#checkall").attr("checked",true);
							}
						}
				});
	 };
	 
    _callback["gift-exchange"] = function(parms) {
				$("#start-time,#end-time,#start-time2,#end-time2").datepicker({dateFormat:magic.dateFormat,monthNames:magic.monthNames,dayNamesMin:magic.dayNamesMin});
				
				function checkSelect(){
						if($("#virtual").attr("selected")){
								$("#virtual-type").css("display","inline");
								$("#virtual-num").css("display","block");
								$("#entity-type").css("display","none");
						}else{
								$("#virtual-type").css("display","none");
								$("#virtual-num").css("display","none");
								$("#entity-type").css("display","inline");
						}
				}
				function checkActivityType(){
						if($("#select-exchange").attr("checked")){
								if(!$.browser.msie){
									$(".items-exchange").css("display","table-row");
								}else{
									$(".items-exchange").css("display","block");
								}
								$(".items-award").css("display","none");
						}else{
								$(".items-exchange").css("display","none");
								if(!$.browser.msie){
									$(".items-award").css("display","table-row");
								}else{
									$(".items-award").css("display","block");
								}
						}
				}
				function checkIfStrive(){
						if($("#select-strive-true").attr("checked")){
								$("#exchange-time-wrap").css("display","block");
						}else{
								$("#exchange-time-wrap").css("display","none");
						}
				}
				checkSelect();
				checkActivityType();
				checkIfStrive();
				$("#gift-type").change(checkSelect);
				$(".select-activity-type").click(checkActivityType);
				$("#select-strive-true").click(checkIfStrive);
				$("#select-strive-false").click(checkIfStrive);
				
		};
		
    _callback["gift-show-area-search"] = function(parms) {
				var fatherLevelWrap = $("#father-level-wrap");
				$(":radio").click(function(){
						if($(this).val() == "child"){
								fatherLevelWrap.css("display","block");
						}else{
								fatherLevelWrap.css("display","none");
						}
				});
				if($(":radio:checked").val() == "child"){
						fatherLevelWrap.css("display","block");
				}
		};
})(magic);